/**
 * Fight module
 */
app.fight = {
	// Log element
	_$log : null,
	_$stage : null,
	
	/**
	 * Module initialization
	 */
	init : function() {
		var that = this;
		
		this._$stage = $('#fightStage');
		this._$log = $('#fightLog');
		
		/**
		 * Bind to events
		 */
		$(app).bind({
			/**
			 * Bind to 'fightinit' event
			 *
			 * @param event Event object
			 * @param enemyType Type of enemy to fight with
			 */
			'fightinit' : function(event, enemyType) {
				that.startFight(enemyType);
			},
			
			/**
			 * Bind to 'locationchange' event
			 *
			 * @param event Event object
			 * @param area Area
			 * @param location Location
			 */
			'locationchange' : function(event, area, location) {
				var enemyType = that.checkFightProbability(area, location);
				
				if (enemyType) {
					that.startFight(enemyType);
				}
			}
		});
		
		
	},
	
	/**
	 * Check fight probability for given location of current area
	 * If the location has exclusive enemies then we will choose from location, in other case coose from area
	 *
	 * @param area
	 * @param location
	 * @return 'false' if no fight will be initiated or enemy type ID
	 */
	checkFightProbability : function(area, location) {
		var enemies = null, fightChance, enemyType;
		
		if (location && location.enemies) {
			enemies = location.enemies;
		}
		else if (area.enemies) {
			enemies = area.enemies;
		}
		
		if (!enemies) {
			return false;
		}
		
		fightChance = Math.floor(Math.random() * 100);
		
		for (var percent in enemies) {
			if (percent < fightChance) {
				continue;
			}

			enemyType = enemies[percent];
			
			if (percent > fightChance) {
				break;
			}
		}
		
		return enemyType || false;
	},
	
	/**
	 * Start a new fight sequence
	 *
	 * @param enemyType A type of enemy to fight with
	 */
	startFight : function(enemyType) {
		var enemy, s1, s2, that = this, damage;
		
		enemy = app.UnitsMgr.createUnit(enemyType);
		
		if (Math.random() <= .5) {
			s1 = app._player;
			s2 = enemy;
		}
		else {
			s1 = enemy;
			s2 = app._player;
		}
		
		this.clearLog();
		this.log(s1.name + ' attacks ' + s2.name + '!');
		
		do {
			if (Math.random() <= s1.accuracy) {
				damage = s1.getDamage();
				
				s2.health -= damage;
				this.log(s1.name + ' hits ' + s2.name + ' for ' + damage + ' damage!');
			}
			else {
				this.log(s1.name + ' failed to hit ' + s2.name);
			}
			
			if (s2.isDead()) {
				this.log(s2.name + ' dies');
				break;
			}
			
			var tmp = s1;
			s1 = s2;
			s2 = tmp;
		} while (true);
		
		app.areas.freezeStage();
		
		this._$log.fadeIn('fast').delay(2000).fadeOut('slow', function() {
			app.areas.unfreezeStage();
		});
		
		return true;
	},
	
	/**
	 * Clear log
	 */
	clearLog : function() {
		this._$log.empty();
	},
	
	/**
	 * Add a message to log
	 *
	 * @param msg Message to add
	 */
	log : function(msg) {
		$('<div>').html(msg).appendTo(this._$log);
	}
};

/**
 * Fight factory class
 */
app.FightMgr = {
	
};